//
//  TWEveryDayViewController.swift
//  EyepetizerImitative
//
//  Created by 田伟 on 2017/4/24.
//  Copyright © 2017年 个人. All rights reserved.
//

import UIKit

class TWEveryDayViewController: TWRootTableViewController {
    fileprivate lazy var shareViewAnimator : TWShareViewAnimator = TWShareViewAnimator()
    override func viewDidLoad() {
        super.viewDidLoad()

        urlString = "https://baobab.kaiyanapp.com/api/v4/tabs/selected?_s=3d878d5c1141e7dea3e8b36e1d399a85&f=iphone&net=wifi&p_product=EYEPETIZER_IOS&u=27e73f129dc6a90ed0b2da2cb1c8b3d14829761d&v=3.3.0&vc=2412"
    }
    
    override func setupNavigationBar() {
        // 设置右边
        let rightButton = UIButton(type: .custom)
        rightButton.setImage(UIImage(named: "nav_share_44x44_"), for: .normal)
        rightButton.addTarget(self, action: #selector(rightShareBarButtonItemDidClick), for: .touchUpInside)
        rightButton.sizeToFit()
        // 往导航栏右边添加一个间隙
        let rightItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        rightItem.width = -15
        navigationItem.rightBarButtonItems = [rightItem,UIBarButtonItem(customView: rightButton)]
        
        // 设置中间的标题栏
        let titleLabel : UILabel = UILabel()
        titleLabel.text = "每日编辑精选"
        titleLabel.font = UIFont(name: "FZLTZCHJW--GB1-0", size: 16)
        titleLabel.textColor = UIColor.black
        titleLabel.sizeToFit()
        navigationItem.titleView = titleLabel
    }
    
    override func setupTableView() {
        super.setupTableView()
        tableView.contentInset = UIEdgeInsets(top: NavigationBarHeight, left: 0, bottom: 0, right: 0)
    }
}

extension TWEveryDayViewController {
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let data = itemsArray[indexPath.row] as! [String : AnyObject]
        let key = data.keys.first!
        // 只展示video
        if key == video {
            return VideoCellHeight
        } else {
            return 0
        }
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let data = itemsArray[indexPath.row] as! [String : AnyObject]
        let key = data.keys.first!
        // 只展示video
        if key == video {
            let cell = tableView.dequeueReusableCell(withIdentifier: VideoCell, for: indexPath) as! TWChoicenessItemCell
            cell.selectionStyle = .none
            let itemModel = data.values.first as! TWItemModel
            cell.itemModel = itemModel
            return cell
        } else {
            return UITableViewCell()
        }
    }
}

extension TWEveryDayViewController {
    @objc fileprivate func rightShareBarButtonItemDidClick(){
        let qrCode = UIStoryboard.init(name: "QRCode", bundle: nil)
        let qrCodeVc = qrCode.instantiateInitialViewController()
        self.navigationController?.pushViewController(qrCodeVc!, animated: true)
        
        // 此处暂时添加一个分享按钮
//        let shareVc : TWShareViewController = TWShareViewController()
//        shareVc.modalPresentationStyle = .custom
//        shareVc.transitioningDelegate = shareViewAnimator
//        shareViewAnimator.presentedFrame = CGRect(x: 0, y: TWScreenHeight - 300, width: TWScreenWidth, height: 300)
////        shareVc.model = model
//        present(shareVc, animated: true, completion: nil)
        
    }
}
